package com.zhihuiyanche.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
  

  
public class LoginInterceptor implements HandlerInterceptor {  
  
    private static final String LOGIN_URL = "toLogin";  
  
    public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object handler) throws Exception {  
    	String userAgent = req.getHeader("user-agent").toLowerCase();
    	if(userAgent.indexOf("micromessenger")>-1){//微信客户端
    		return true; 
    	}
    	String url = req.getRequestURI();
    	if(url.indexOf("/com-zhihuiyanche-web/toLogin") > -1){
    		return true; 
    	}
    	if(url.indexOf("/com-zhihuiyanche-web/signUp") > -1){
    		return true; 
    	}
    	if(url.indexOf("/com-zhihuiyanche-web/getUser") > -1){
    		return true; 
    	}
    	if(url.indexOf("/com-zhihuiyanche-web/insertUser") > -1){
    		return true; 
    	}
    	
        HttpSession session = req.getSession(true);  
        Object obj = session.getAttribute("shopId");  
        if (obj == null || "".equals(obj.toString())) {  
            res.sendRedirect(LOGIN_URL);  
            return false;
        }  
        return true;  
    }  
  
    public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3) throws Exception {  
    }  
  
    public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception {  
    }  
  
}  